class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n < 1:
            return False

        s = set()

        while (n != 1) and (n not in s):
            s.add(n)
            newN = 0
            while n > 0:
                tmp = n % 10
                n = n // 10
                newN += tmp * tmp

            n = newN

        return n == 1


s = Solution()
print(s.isHappy(19))
print(s.isHappy(4))
